package ift604.projet.music;

import ift604.projet.MapMyFeelingsActivity;
import ift604.projet.MapMyFeelingsApplication;
import ift604.projet.R;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MusicPlayer {

    private static RelativeLayout musicPlayer;

    public static RelativeLayout musicPlayer(Activity activity){
        return (RelativeLayout) activity.findViewById(R.id.music_player);
    }
    
    public static void displayConnectWithStereoMood(Activity activity){

    	toggleAll(activity, 1);
    	
        musicPlayer = musicPlayer(activity);
        musicPlayer.setVisibility(View.VISIBLE);
    }

    public static void displayLoadingOnMusicPlayer(Activity activity){
    	toggleAll(activity, 2);
    }

    public static void displayMusicPlayer(final Activity activity, Playlist playlist){
    	toggleAll(activity, 3);
    	updateSongLabelWithSong(playlist.currentlyPlayingSong(), activity);

    	// Buttons
    	TextView playButton = (TextView) activity.findViewById(R.id.mp_play);
    	TextView pauseButton = (TextView) activity.findViewById(R.id.mp_pause);
    	TextView nextTrackButton = (TextView) activity.findViewById(R.id.mp_next);
    	TextView listButton = (TextView) activity.findViewById(R.id.mp_list);
    	
    	// Click listeners
    	playButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                Activity activity = (Activity) view.getContext();
                MapMyFeelingsApplication mapMyFeelingsApplication = (MapMyFeelingsApplication) activity.getApplication();
                activity = mapMyFeelingsApplication.mapMyFeelingsActivity;
                Playlist currentPlaylist = mapMyFeelingsApplication.currentPlaylist();
                
                currentPlaylist.resume();
            }
        });
    	
    	pauseButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                Activity activity = (Activity) view.getContext();
                MapMyFeelingsApplication mapMyFeelingsApplication = (MapMyFeelingsApplication) activity.getApplication();
                activity = mapMyFeelingsApplication.mapMyFeelingsActivity;
                Playlist currentPlaylist = mapMyFeelingsApplication.currentPlaylist();
                
                currentPlaylist.pause();
            }
        });
    	
    	nextTrackButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                Activity activity = (Activity) view.getContext();
                MapMyFeelingsApplication mapMyFeelingsApplication = (MapMyFeelingsApplication) activity.getApplication();
                activity = mapMyFeelingsApplication.mapMyFeelingsActivity;
                Playlist currentPlaylist = mapMyFeelingsApplication.currentPlaylist();
                Song currentlyPlayingSong = currentPlaylist.playNextSong();
                updateSongLabelWithSong(currentlyPlayingSong, activity);
            }
        });
    	
    	listButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
				Intent songsList = new Intent(activity.getBaseContext(), SelectSongActivity.class);
				activity.startActivityForResult(songsList, MapMyFeelingsActivity.SONG_SELECTION_REQUEST);
            }
        });
    }
    
    
    public static void updateSongLabelWithSong(Song song, Activity activity){
    	TextView songtitle = (TextView) activity.findViewById(R.id.songtitle);
    	songtitle.setText(song.title());
    }
    
    private static void toggleAll(Activity activity, int etape)
    {
    	// Views
    	TextView getplaylist = (TextView) activity.findViewById(R.id.getPlaylist);
    	TextView getloading = (TextView) activity.findViewById(R.id.getLoading);
    	RelativeLayout mptools = (RelativeLayout) activity.findViewById(R.id.music_player_tool);
    	TextView songtitle = (TextView) activity.findViewById(R.id.songtitle);
    	
    	// Visible or gone
    	getplaylist.setVisibility((etape == 1 ? View.VISIBLE : View.GONE));
    	getloading.setVisibility((etape == 2 ? View.VISIBLE : View.GONE));
    	mptools.setVisibility((etape == 3 ? View.VISIBLE : View.GONE));
    	songtitle.setVisibility((etape == 3 ? View.VISIBLE : View.GONE));
    		
    }

}
